﻿using AutoMapper;
using danreng.Domain;
using sharp_net;
using sharp_net.Repositories;
using sharp_net.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using danreng.Enum;
using sharp_net.Repositories.MongoDB;

namespace danreng.Application {
    public interface IItemService {
        ItemData Get(string ID);
        IEnumerable<ItemData> GetListPager(ref int count, int pageSize = 10, int pageIndex = 1);
        ItemData Creat(ItemData dataObject, string userKey);
        void Update(ItemData dataObject);
    }

    public class ItemApplication : BaseApplication, IItemService {

        private IRepositoryContext Context { 
            get { return IocLocator.Instance.GetImple<IRepositoryContext>(eDbType.Mongo.ToString(), new { settingkey = eDbHost.Mongo_Main.ToString() }); } 
        }

        public ItemData Get(string id) {
            using (var context = Context) {
                var repo = context.GetRepository<Item>();
                var item = repo.GetById(id);
                if (item == null)
                    return null;
                var imgData = Mapper.Map<Item, ItemData>(item);
                return imgData;
            }
        }

        public IEnumerable<ItemData> GetListPager(ref int count, int pageSize = 10, int pageIndex = 1) {
            using (var context = Context) {
                var repo = context.GetRepository<Item>();
                var items = repo.FindAll(pageIndex, pageSize);
                if (items == null || items.Count() == 0)
                    return null;
                count = repo.FindAll().Count();
                var itemDatas = new List<ItemData>();
                foreach (var item in items) {
                    itemDatas.Add(Mapper.Map<Item, ItemData>(item));
                }
                return itemDatas;
            }
        }

        public ItemData Creat(ItemData dataObject, string userKey) {
            if (dataObject == null)
                throw new ArgumentNullException("itemDataObject");
            //var userApplication = IocLocator.Instance.GetImple<IItemService>();
            //UserData user = userApplication.GetByKey(userKey);
            dataObject.UserId = userKey;
            using (var context = Context) {
                var repo = context.GetRepository<Item>();
                Item item = Mapper.Map<ItemData, Item>(dataObject);
                item.Creat();
                repo.Add(item);
                context.Commit();
                return Mapper.Map<Item, ItemData>(item);
            }
        }

        public void Update(ItemData data) {
            if (string.IsNullOrEmpty(data.Id))
                throw new ArgumentNullException("ID");
            using (var context = Context) {
                var repo = context.GetRepository<Item>();
                var item = repo.Get(Specification<Item>.Eval(c => c.Id.ToString() == data.Id));
                if (data.ActEnum == (int)eAct.Delete) {
                    repo.Remove(item);
                    return;
                }
                item.Update(data);
                repo.Update(item);
            }
        }
    }
}